#include<iostream>
using namespace std;
#define MAXSIZE 6
typedef struct node{
    int * base;// 一个数组用于存储整个循环队列
    int front;
    int rear;
}Node;
class CircleQueue{
    public:

    Node CQueue;
    CircleQueue();
    int queueLength();
    void pushQueue(int data);
    int popQueue();
    int isfull();
    void showInfo();
};
int main(){
    CircleQueue CQ;
    CQ.pushQueue(1);
    CQ.pushQueue(2);
    CQ.pushQueue(3);
    CQ.pushQueue(4);
    cout<<"队列中的元素个数为"<<CQ.queueLength()<<endl;
   cout<<"出对了"<<CQ.popQueue()<<endl;
   cout<<"出对了"<<CQ.popQueue()<<endl;
   cout<<"出对了"<<CQ.popQueue()<<endl;
   cout<<"出对了"<<CQ.popQueue()<<endl;
   cout<<"出对了"<<CQ.popQueue()<<endl;
    return 0;
}
CircleQueue::CircleQueue(){
        this->CQueue.base=new int[MAXSIZE];
        CQueue.front=0;
        CQueue.rear=0;
        
}
int CircleQueue::queueLength(){
    return (CQueue.rear-CQueue.front+MAXSIZE)%MAXSIZE;// 队列长度  
}
int CircleQueue::isfull(){
    if((CQueue.rear-CQueue.front+MAXSIZE)%MAXSIZE==MAXSIZE-1){
        return 1;
    }
    else{
        return 0;
    }
}
void CircleQueue::pushQueue(int data){
    if(this->isfull()==1){
        cout<<"队列满,入队失败"<<endl;
    }
    else{
        CQueue.base[CQueue.rear]=data;// rear不会超出索引
        CQueue.rear=(CQueue.rear+1)%MAXSIZE; // 如果rear=maxisize-1,该条语句使得rear=0
    }
}
int CircleQueue::popQueue(){
    if(CQueue.rear==CQueue.front){
        cout<<"队列为空"<<endl;
        return -1;
    }
    else{
        int index=CQueue.front;
        CQueue.front=(CQueue.front+1)%MAXSIZE; //只要可以把front由maxsize-1变为0即可
        return CQueue.base[index];
    }
}
void CircleQueue::showInfo(){
        for(int i=0;i<(CQueue.rear-CQueue.front+MAXSIZE)%MAXSIZE;i++){
                cout<<CQueue.base[(CQueue.front+i+1)%MAXSIZE]<<" ";
        }
}
